package com.power.sifa.flowable.controller;

import com.plf.common.domain.ResponseDTO;
import com.plf.common.exception.BusinessException;
import com.plf.common.utils.SpringHolder;
import com.plf.core.base.model.Ha_datasource;
import com.plf.core.base.service.Ha_calendarService;
import com.plf.core.base.service.Ha_datasourceService;
import com.plf.core.base.service.Ha_formService;
import com.power.sifa.util.executor.IPostExecutor;
import com.power.sifa.flowable.api.IFlowableSfapprove_toolFeign;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "FlowableYjaprv_toolController", tags = {"工作流feign"})
public class FlowableSfapprove_toolController implements IFlowableSfapprove_toolFeign {

    @Resource
    private Ha_calendarService ha_calendarService;
    @Resource
    private Ha_datasourceService ha_datasourceService;
    @Resource
    private Ha_formService ha_formService;

    @Override
    public Long durationTime(String starttime, String endtime, String result) {
        try {
            return this.ha_calendarService.durationTime(starttime, endtime, result);
        } catch (Exception e) {
            log.error("/flowable/durationTime 计算除去工作日的任务耗时失败 ->  msg[{}]", e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseDTO saveFormData(Map<String, Object> body) {
        try {
            return ResponseDTO.succData(String.valueOf(this.ha_formService.saveFormData(body)));
        } catch (Exception e) {
            log.error("/flowable/saveFormData 保存流程表单数据失败");
            e.printStackTrace();
            return ResponseDTO.wrap(500, "保存流程表单数据失败：" + e.getMessage());
        }
    }

    @Override
    public Ha_datasource datasourceView(Integer id) {
        try {
            return this.ha_datasourceService.getById(id);
        } catch (Exception e) {
            log.error("/flowable/datasourceView 查看表单所用的数据源失败 ->  msg[{}]", e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseDTO executeService(String processinstanceid, String taskid, String stepid, String applyid, List<Map<String, Object>> serveList) {
        try {
            IPostExecutor postexec;
            // 执行服务返回值
            Map<String, Object> backParams = new HashMap<>(16);
            for (Map<String, Object> config : serveList) {
                // 执行按钮配置的服务
                if (config.get("bean") != null) {
                    postexec = (IPostExecutor) SpringHolder.getBean((String) config.get("bean"));
                    postexec.setProcessinstanceid(processinstanceid);
                    postexec.setTaskid(taskid);
                    postexec.setStepid(stepid);
                    postexec.setapplyid(applyid);
                    postexec.init((Map<String, Object>) config.get("params"));
                    try {
                        // 把返回参数返回给前端
                        Map<String, Object> params = postexec.execute();
                        if (null != params) {
                            for (Map.Entry entry : params.entrySet()) {
                                backParams.put((String) entry.getKey(), entry.getValue());
                            }

                        }
                        log.info(config.get("bean").toString() + "执行成功");
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(config.get("bean").toString() + "执行失败");
                        log.error("msg: [{}]", e.getMessage());
                        throw new BusinessException(e.getMessage());
                    }
                }
            }
            return ResponseDTO.succData(backParams);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/flowable/executeService 执行流程按钮配置的服务失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO getDatasourceByname(String datasource) {
        Ha_datasource soureObj = this.ha_datasourceService.getByDatabase(datasource);
        if (null == soureObj) {
            log.error("/flowable/getDatasourceByname 查询数据源失败");
            return ResponseDTO.wrap(500, "查询数据源失败");
        }
        return ResponseDTO.succData(soureObj.getId());
    }

}
